home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tk / generic / tkEntry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-31  |  69.9 KB  |  2,294 lines

  1. /* 
  2.  * tkEntry.c --
  3.  *
  4.  *    This module implements entry widgets for the Tk
  5.  *    toolkit.  An entry displays a string and allows
  6.  *    the string to be edited.
  7.  *
  8.  * Copyright (c) 1990-1994 The Regents of the University of California.
  9.  * Copyright (c) 1994-1996 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  *
  14.  * SCCS: @(#) tkEntry.c 1.101 96/03/21 13:55:45
  15.  */
  16.  
  17. #include "default.h"
  18. #include "tkPort.h"
  19. #include "tkInt.h"
  20.  
  21. /*
  22.  * A data structure of the following type is kept for each entry
  23.  * widget managed by this file:
  24.  */
  25.  
  26. typedef struct {
  27.     Tk_Window tkwin;        /* Window that embodies the entry. NULL
  28.                  * means that the window has been destroyed
  29.                  * but the data structures haven't yet been
  30.                  * cleaned up.*/
  31.     Display *display;        /* Display containing widget.  Used, among
  32.                  * other things, so that resources can be
  33.                  * freed even after tkwin has gone away. */
  34.     Tcl_Interp *interp;        /* Interpreter associated with entry. */
  35.     Tcl_Command widgetCmd;    /* Token for entry's widget command. */
  36.     int numChars;        /* Number of non-NULL characters in
  37.                  * string (may be 0). */
  38.     char *string;        /* Pointer to storage for string;
  39.                  * NULL-terminated;  malloc-ed. */
  40.     char *textVarName;        /* Name of variable (malloc'ed) or NULL.
  41.                  * If non-NULL, entry's string tracks the
  42.                  * contents of this variable and vice versa. */
  43.     Tk_Uid state;        /* Normal or disabled.  Entry is read-only
  44.                  * when disabled. */
  45.  
  46.     /*
  47.      * Information used when displaying widget:
  48.      */
  49.  
  50.     Tk_3DBorder normalBorder;    /* Used for drawing border around whole
  51.                  * window, plus used for background. */
  52.     int borderWidth;        /* Width of 3-D border around window. */
  53.     int relief;            /* 3-D effect: TK_RELIEF_RAISED, etc. */
  54.     XFontStruct *fontPtr;    /* Information about text font, or NULL. */
  55.     XColor *fgColorPtr;        /* Text color in normal mode. */
  56.     GC textGC;            /* For drawing normal text. */
  57.     Tk_3DBorder selBorder;    /* Border and background for selected
  58.                  * characters. */
  59.     int selBorderWidth;        /* Width of border around selection. */
  60.     XColor *selFgColorPtr;    /* Foreground color for selected text. */
  61.     GC selTextGC;        /* For drawing selected text. */
  62.     Tk_3DBorder insertBorder;    /* Used to draw vertical bar for insertion
  63.                  * cursor. */
  64.     int insertWidth;        /* Total width of insert cursor. */
  65.     int insertBorderWidth;    /* Width of 3-D border around insert cursor. */
  66.     int insertOnTime;        /* Number of milliseconds cursor should spend
  67.                  * in "on" state for each blink. */
  68.     int insertOffTime;        /* Number of milliseconds cursor should spend
  69.                  * in "off" state for each blink. */
  70.     Tcl_TimerToken insertBlinkHandler;
  71.                 /* Timer handler used to blink cursor on and
  72.                  * off. */
  73.     int highlightWidth;        /* Width in pixels of highlight to draw
  74.                  * around widget when it has the focus.
  75.                  * <= 0 means don't draw a highlight. */
  76.     XColor *highlightBgColorPtr;
  77.                 /* Color for drawing traversal highlight
  78.                  * area when highlight is off. */
  79.     XColor *highlightColorPtr;    /* Color for drawing traversal highlight. */
  80.     GC highlightGC;        /* For drawing traversal highlight. */
  81.     Tk_Justify justify;        /* Justification to use for text within
  82.                  * window. */
  83.     int avgWidth;        /* Width of average character. */
  84.     int prefWidth;        /* Desired width of window, measured in
  85.                  * average characters. */
  86.     int inset;            /* Number of pixels on the left and right
  87.                  * sides that are taken up by XPAD, borderWidth
  88.                  * (if any), and highlightWidth (if any). */
  89.     int leftIndex;        /* Index of left-most character visible in
  90.                  * window. */
  91.     int leftX;            /* X position at which leftIndex is drawn
  92.                  * (varies depending on justify). */
  93.     int tabOrigin;        /* Origin for tabs (left edge of string[0]). */
  94.     int insertPos;        /* Index of character before which next
  95.                  * typed character will be inserted. */
  96.     char *showChar;        /* Value of -show option.  If non-NULL, first
  97.                  * character is used for displaying all
  98.                  * characters in entry.  Malloc'ed. */
  99.     char *displayString;    /* If non-NULL, points to string with same
  100.                  * length as string but whose characters
  101.                  * are all equal to showChar.  Malloc'ed. */
  102.  
  103.     /*
  104.      * Information about what's selected, if any.
  105.      */
  106.  
  107.     int selectFirst;        /* Index of first selected character (-1 means
  108.                  * nothing selected. */
  109.     int selectLast;        /* Index of last selected character (-1 means
  110.                  * nothing selected. */
  111.     int selectAnchor;        /* Fixed end of selection (i.e. "select to"
  112.                  * operation will use this as one end of the
  113.                  * selection). */
  114.     int exportSelection;    /* Non-zero means tie internal entry selection
  115.                  * to X selection. */
  116.  
  117.     /*
  118.      * Information for scanning:
  119.      */
  120.  
  121.     int scanMarkX;        /* X-position at which scan started (e.g.
  122.                  * button was pressed here). */
  123.     int scanMarkIndex;        /* Index of character that was at left of
  124.                  * window when scan started. */
  125.  
  126.     /*
  127.      * Miscellaneous information:
  128.      */
  129.  
  130.     Tk_Cursor cursor;        /* Current cursor for window, or None. */
  131.     char *takeFocus;        /* Value of -takefocus option;  not used in
  132.                  * the C code, but used by keyboard traversal
  133.                  * scripts.  Malloc'ed, but may be NULL. */
  134.     char *scrollCmd;        /* Command prefix for communicating with
  135.                  * scrollbar(s).  Malloc'ed.  NULL means
  136.                  * no command to issue. */
  137.     int flags;            /* Miscellaneous flags;  see below for
  138.                  * definitions. */
  139. #ifdef STk_CODE
  140.     int stringp;        /* if true the value of the variable associated
  141.                  * to the entry must be "stringified" */
  142. #endif
  143. } Entry;
  144.  
  145. /*
  146.  * Assigned bits of "flags" fields of Entry structures, and what those
  147.  * bits mean:
  148.  *
  149.  * REDRAW_PENDING:        Non-zero means a DoWhenIdle handler has
  150.  *                already been queued to redisplay the entry.
  151.  * BORDER_NEEDED:        Non-zero means 3-D border must be redrawn
  152.  *                around window during redisplay.  Normally
  153.  *                only text portion needs to be redrawn.
  154.  * CURSOR_ON:            Non-zero means insert cursor is displayed at
  155.  *                present.  0 means it isn't displayed.
  156.  * GOT_FOCUS:            Non-zero means this window has the input
  157.  *                focus.
  158.  * UPDATE_SCROLLBAR:        Non-zero means scrollbar should be updated
  159.  *                during next redisplay operation.
  160.  */
  161.  
  162. #define REDRAW_PENDING        1
  163. #define BORDER_NEEDED        2
  164. #define CURSOR_ON        4
  165. #define GOT_FOCUS        8
  166. #define UPDATE_SCROLLBAR    16
  167.  
  168. /*
  169.  * The following macro defines how many extra pixels to leave on each
  170.  * side of the text in the entry.
  171.  */
  172.  
  173. #define XPAD 1
  174. #define YPAD 1
  175.  
  176. /*
  177.  * Information used for argv parsing.
  178.  */
  179.  
  180. static Tk_ConfigSpec configSpecs[] = {
  181.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  182.     DEF_ENTRY_BG_COLOR, Tk_Offset(Entry, normalBorder),
  183.     TK_CONFIG_COLOR_ONLY},
  184.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  185.     DEF_ENTRY_BG_MONO, Tk_Offset(Entry, normalBorder),
  186.     TK_CONFIG_MONO_ONLY},
  187.     {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
  188.     (char *) NULL, 0, 0},
  189.     {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
  190.     (char *) NULL, 0, 0},
  191.     {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
  192.     DEF_ENTRY_BORDER_WIDTH, Tk_Offset(Entry, borderWidth), 0},
  193.     {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
  194.     DEF_ENTRY_CURSOR, Tk_Offset(Entry, cursor), TK_CONFIG_NULL_OK},
  195.     {TK_CONFIG_BOOLEAN, "-exportselection", "exportSelection",
  196.     "ExportSelection", DEF_ENTRY_EXPORT_SELECTION,
  197.     Tk_Offset(Entry, exportSelection), 0},
  198.     {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
  199.     (char *) NULL, 0, 0},
  200.     {TK_CONFIG_FONT, "-font", "font", "Font",
  201.     DEF_ENTRY_FONT, Tk_Offset(Entry, fontPtr), 0},
  202.     {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
  203.     DEF_ENTRY_FG, Tk_Offset(Entry, fgColorPtr), 0},
  204.     {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
  205.     "HighlightBackground", DEF_ENTRY_HIGHLIGHT_BG,
  206.     Tk_Offset(Entry, highlightBgColorPtr), 0},
  207.     {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
  208.     DEF_ENTRY_HIGHLIGHT, Tk_Offset(Entry, highlightColorPtr), 0},
  209.     {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
  210.     "HighlightThickness",
  211.     DEF_ENTRY_HIGHLIGHT_WIDTH, Tk_Offset(Entry, highlightWidth), 0},
  212.     {TK_CONFIG_BORDER, "-insertbackground", "insertBackground", "Foreground",
  213.     DEF_ENTRY_INSERT_BG, Tk_Offset(Entry, insertBorder), 0},
  214.     {TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth",
  215.     DEF_ENTRY_INSERT_BD_COLOR, Tk_Offset(Entry, insertBorderWidth),
  216.     TK_CONFIG_COLOR_ONLY},
  217.     {TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth",
  218.     DEF_ENTRY_INSERT_BD_MONO, Tk_Offset(Entry, insertBorderWidth),
  219.     TK_CONFIG_MONO_ONLY},
  220.     {TK_CONFIG_INT, "-insertofftime", "insertOffTime", "OffTime",
  221.     DEF_ENTRY_INSERT_OFF_TIME, Tk_Offset(Entry, insertOffTime), 0},
  222.     {TK_CONFIG_INT, "-insertontime", "insertOnTime", "OnTime",
  223.     DEF_ENTRY_INSERT_ON_TIME, Tk_Offset(Entry, insertOnTime), 0},
  224.     {TK_CONFIG_PIXELS, "-insertwidth", "insertWidth", "InsertWidth",
  225.     DEF_ENTRY_INSERT_WIDTH, Tk_Offset(Entry, insertWidth), 0},
  226.     {TK_CONFIG_JUSTIFY, "-justify", "justify", "Justify",
  227.     DEF_ENTRY_JUSTIFY, Tk_Offset(Entry, justify), 0},
  228.     {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
  229.     DEF_ENTRY_RELIEF, Tk_Offset(Entry, relief), 0},
  230.     {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
  231.     DEF_ENTRY_SELECT_COLOR, Tk_Offset(Entry, selBorder),
  232.     TK_CONFIG_COLOR_ONLY},
  233.     {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
  234.     DEF_ENTRY_SELECT_MONO, Tk_Offset(Entry, selBorder),
  235.     TK_CONFIG_MONO_ONLY},
  236.     {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
  237.     DEF_ENTRY_SELECT_BD_COLOR, Tk_Offset(Entry, selBorderWidth),
  238.     TK_CONFIG_COLOR_ONLY},
  239.     {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
  240.     DEF_ENTRY_SELECT_BD_MONO, Tk_Offset(Entry, selBorderWidth),
  241.     TK_CONFIG_MONO_ONLY},
  242.     {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
  243.     DEF_ENTRY_SELECT_FG_COLOR, Tk_Offset(Entry, selFgColorPtr),
  244.     TK_CONFIG_COLOR_ONLY},
  245.     {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
  246.     DEF_ENTRY_SELECT_FG_MONO, Tk_Offset(Entry, selFgColorPtr),
  247.     TK_CONFIG_MONO_ONLY},
  248.     {TK_CONFIG_STRING, "-show", "show", "Show",
  249.     DEF_ENTRY_SHOW, Tk_Offset(Entry, showChar), TK_CONFIG_NULL_OK},
  250.     {TK_CONFIG_UID, "-state", "state", "State",
  251.     DEF_ENTRY_STATE, Tk_Offset(Entry, state), 0},
  252. #ifdef STk_CODE
  253.     {TK_CONFIG_BOOLEAN, "-stringvalue", "stringvalue", "StringValue",
  254.     "#t", Tk_Offset(Entry, stringp), 0},
  255. #endif
  256. #ifdef STk_CODE
  257.     {TK_CONFIG_CLOSURE, "-takefocus", "takeFocus", "TakeFocus",
  258. #else
  259.     {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
  260. #endif
  261.     DEF_ENTRY_TAKE_FOCUS, Tk_Offset(Entry, takeFocus), TK_CONFIG_NULL_OK},
  262.     {TK_CONFIG_STRING, "-textvariable", "textVariable", "Variable",
  263.     DEF_ENTRY_TEXT_VARIABLE, Tk_Offset(Entry, textVarName),
  264.     TK_CONFIG_NULL_OK},
  265.     {TK_CONFIG_INT, "-width", "width", "Width",
  266.     DEF_ENTRY_WIDTH, Tk_Offset(Entry, prefWidth), 0},
  267. #ifdef STk_CODE
  268.     {TK_CONFIG_CLOSURE, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
  269. #else
  270.     {TK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
  271. #endif
  272.     DEF_ENTRY_SCROLL_COMMAND, Tk_Offset(Entry, scrollCmd),
  273.     TK_CONFIG_NULL_OK},
  274.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  275.     (char *) NULL, 0, 0}
  276. };
  277.  
  278. /*
  279.  * Flags for GetEntryIndex procedure:
  280.  */
  281.  
  282. #define ZERO_OK            1
  283. #define LAST_PLUS_ONE_OK    2
  284.  
  285. /*
  286.  * Forward declarations for procedures defined later in this file:
  287.  */
  288.  
  289. static int        ConfigureEntry _ANSI_ARGS_((Tcl_Interp *interp,
  290.                 Entry *entryPtr, int argc, char **argv,
  291.                 int flags));
  292. static void        DeleteChars _ANSI_ARGS_((Entry *entryPtr, int index,
  293.                 int count));
  294. static void        DestroyEntry _ANSI_ARGS_((char *memPtr));
  295. static void        DisplayEntry _ANSI_ARGS_((ClientData clientData));
  296. static void        EntryBlinkProc _ANSI_ARGS_((ClientData clientData));
  297. static void        EntryCmdDeletedProc _ANSI_ARGS_((
  298.                 ClientData clientData));
  299. static void        EntryComputeGeometry _ANSI_ARGS_((Entry *entryPtr));
  300. static void        EntryEventProc _ANSI_ARGS_((ClientData clientData,
  301.                 XEvent *eventPtr));
  302. static void        EntryFocusProc _ANSI_ARGS_ ((Entry *entryPtr,
  303.                 int gotFocus));
  304. static int        EntryFetchSelection _ANSI_ARGS_((ClientData clientData,
  305.                 int offset, char *buffer, int maxBytes));
  306. static void        EntryLostSelection _ANSI_ARGS_((
  307.                 ClientData clientData));
  308. static void        EventuallyRedraw _ANSI_ARGS_((Entry *entryPtr));
  309. static void        EntryScanTo _ANSI_ARGS_((Entry *entryPtr, int y));
  310. static void        EntrySetValue _ANSI_ARGS_((Entry *entryPtr,
  311.                 char *value));
  312. static void        EntrySelectTo _ANSI_ARGS_((
  313.                 Entry *entryPtr, int index));
  314. static char *        EntryTextVarProc _ANSI_ARGS_((ClientData clientData,
  315.                 Tcl_Interp *interp, char *name1, char *name2,
  316.                 int flags));
  317. static void        EntryUpdateScrollbar _ANSI_ARGS_((Entry *entryPtr));
  318. static void        EntryValueChanged _ANSI_ARGS_((Entry *entryPtr));
  319. static void        EntryVisibleRange _ANSI_ARGS_((Entry *entryPtr,
  320.                 double *firstPtr, double *lastPtr));
  321. static int        EntryWidgetCmd _ANSI_ARGS_((ClientData clientData,
  322.                 Tcl_Interp *interp, int argc, char **argv));
  323. static int        GetEntryIndex _ANSI_ARGS_((Tcl_Interp *interp,
  324.                 Entry *entryPtr, char *string, int *indexPtr));
  325. static void        InsertChars _ANSI_ARGS_((Entry *entryPtr, int index,
  326.                 char *string));
  327.  
  328. /*
  329.  *--------------------------------------------------------------
  330.  *
  331.  * Tk_EntryCmd --
  332.  *
  333.  *    This procedure is invoked to process the "entry" Tcl
  334.  *    command.  See the user documentation for details on what
  335.  *    it does.
  336.  *
  337.  * Results:
  338.  *    A standard Tcl result.
  339.  *
  340.  * Side effects:
  341.  *    See the user documentation.
  342.  *
  343.  *--------------------------------------------------------------
  344.  */
  345.  
  346. int
  347. Tk_EntryCmd(clientData, interp, argc, argv)
  348.     ClientData clientData;    /* Main window associated with
  349.                  * interpreter. */
  350.     Tcl_Interp *interp;        /* Current interpreter. */
  351.     int argc;            /* Number of arguments. */
  352.     char **argv;        /* Argument strings. */
  353. {
  354.     Tk_Window tkwin = (Tk_Window) clientData;
  355.     register Entry *entryPtr;
  356.     Tk_Window new;
  357.  
  358.     if (argc < 2) {
  359.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  360.         argv[0], " pathName ?options?\"", (char *) NULL);
  361.     return TCL_ERROR;
  362.     }
  363.  
  364.     new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL);
  365.     if (new == NULL) {
  366.     return TCL_ERROR;
  367.     }
  368.  
  369.     /*
  370.      * Initialize the fields of the structure that won't be initialized
  371.      * by ConfigureEntry, or that ConfigureEntry requires to be
  372.      * initialized already (e.g. resource pointers).
  373.      */
  374.  
  375.     entryPtr = (Entry *) ckalloc(sizeof(Entry));
  376.     entryPtr->tkwin = new;
  377.     entryPtr->display = Tk_Display(new);
  378.     entryPtr->interp = interp;
  379.     entryPtr->widgetCmd = Tcl_CreateCommand(interp,
  380.         Tk_PathName(entryPtr->tkwin), EntryWidgetCmd,
  381.         (ClientData) entryPtr, EntryCmdDeletedProc);
  382.     entryPtr->numChars = 0;
  383.     entryPtr->string = (char *) ckalloc(1);
  384.     entryPtr->string[0] = '\0';
  385.     entryPtr->textVarName = NULL;
  386.     entryPtr->state = tkNormalUid;
  387.     entryPtr->normalBorder = NULL;
  388.     entryPtr->borderWidth = 0;
  389.     entryPtr->relief = TK_RELIEF_FLAT;
  390.     entryPtr->fontPtr = NULL;
  391.     entryPtr->fgColorPtr = NULL;
  392.     entryPtr->textGC = None;
  393.     entryPtr->selBorder = NULL;
  394.     entryPtr->selBorderWidth = 0;
  395.     entryPtr->selFgColorPtr = NULL;
  396.     entryPtr->selTextGC = None;
  397.     entryPtr->insertBorder = NULL;
  398.     entryPtr->insertWidth = 0;
  399.     entryPtr->insertBorderWidth = 0;
  400.     entryPtr->insertOnTime = 0;
  401.     entryPtr->insertOffTime = 0;
  402.     entryPtr->insertBlinkHandler = (Tcl_TimerToken) NULL;
  403.     entryPtr->highlightWidth = 0;
  404.     entryPtr->highlightBgColorPtr = NULL;
  405.     entryPtr->highlightColorPtr = NULL;
  406.     entryPtr->justify = TK_JUSTIFY_LEFT;
  407.     entryPtr->avgWidth = 1;
  408.     entryPtr->prefWidth = 0;
  409.     entryPtr->inset = XPAD;
  410.     entryPtr->leftIndex = 0;
  411.     entryPtr->leftX = 0;
  412.     entryPtr->tabOrigin = 0;
  413.     entryPtr->insertPos = 0;
  414.     entryPtr->showChar = NULL;
  415.     entryPtr->displayString = NULL;
  416.     entryPtr->selectFirst = -1;
  417.     entryPtr->selectLast = -1;
  418.     entryPtr->selectAnchor = 0;
  419.     entryPtr->exportSelection = 1;
  420.     entryPtr->scanMarkX = 0;
  421.     entryPtr->scanMarkIndex = 0;
  422.     entryPtr->cursor = None;
  423.     entryPtr->takeFocus = NULL;
  424.     entryPtr->scrollCmd = NULL;
  425.     entryPtr->flags = 0;
  426.  
  427.     Tk_SetClass(entryPtr->tkwin, "Entry");
  428.     Tk_CreateEventHandler(entryPtr->tkwin,
  429.         ExposureMask|StructureNotifyMask|FocusChangeMask,
  430.         EntryEventProc, (ClientData) entryPtr);
  431.     Tk_CreateSelHandler(entryPtr->tkwin, XA_PRIMARY, XA_STRING,
  432.         EntryFetchSelection, (ClientData) entryPtr, XA_STRING);
  433.     if (ConfigureEntry(interp, entryPtr, argc-2, argv+2, 0) != TCL_OK) {
  434.     goto error;
  435.     }
  436.  
  437. #ifdef STk_CODE
  438.     STk_sharp_dot_result(interp, Tk_PathName(entryPtr->tkwin));
  439. #else
  440.     interp->result = Tk_PathName(entryPtr->tkwin);
  441. #endif
  442.     return TCL_OK;
  443.  
  444.     error:
  445.     Tk_DestroyWindow(entryPtr->tkwin);
  446.     return TCL_ERROR;
  447. }
  448.  
  449. /*
  450.  *--------------------------------------------------------------
  451.  *
  452.  * EntryWidgetCmd --
  453.  *
  454.  *    This procedure is invoked to process the Tcl command
  455.  *    that corresponds to a widget managed by this module.
  456.  *    See the user documentation for details on what it does.
  457.  *
  458.  * Results:
  459.  *    A standard Tcl result.
  460.  *
  461.  * Side effects:
  462.  *    See the user documentation.
  463.  *
  464.  *--------------------------------------------------------------
  465.  */
  466.  
  467. static int
  468. EntryWidgetCmd(clientData, interp, argc, argv)
  469.     ClientData clientData;        /* Information about entry widget. */
  470.     Tcl_Interp *interp;            /* Current interpreter. */
  471.     int argc;                /* Number of arguments. */
  472.     char **argv;            /* Argument strings. */
  473. {
  474.     register Entry *entryPtr = (Entry *) clientData;
  475.     int result = TCL_OK;
  476.     size_t length;
  477.     int c, height;
  478.  
  479.     if (argc < 2) {
  480.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  481.         argv[0], " option ?arg arg ...?\"", (char *) NULL);
  482.     return TCL_ERROR;
  483.     }
  484.     Tcl_Preserve((ClientData) entryPtr);
  485.     c = argv[1][0];
  486.     length = strlen(argv[1]);
  487.     if ((c == 'b') && (strncmp(argv[1], "bbox", length) == 0)) {
  488.     int index, x1, x2;
  489.  
  490.     if (argc != 3) {
  491.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  492.             argv[0], " bbox index\"",
  493.             (char *) NULL);
  494.         goto error;
  495.     }
  496.     if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) {
  497.         goto error;
  498.     }
  499.     if ((index == entryPtr->numChars) && (index > 0)) {
  500.         index--;
  501.     }
  502.     TkMeasureChars(entryPtr->fontPtr,
  503.         (entryPtr->displayString == NULL) ? entryPtr->string
  504.         : entryPtr->displayString, index, entryPtr->tabOrigin,
  505.         1000000, entryPtr->tabOrigin, TK_NEWLINES_NOT_SPECIAL,
  506.         &x1);
  507.     if (index < entryPtr->numChars) {
  508.         TkMeasureChars(entryPtr->fontPtr,
  509.         (entryPtr->displayString == NULL) ? entryPtr->string
  510.         : entryPtr->displayString, index+1, entryPtr->tabOrigin,
  511.         1000000, entryPtr->tabOrigin, TK_NEWLINES_NOT_SPECIAL,
  512.         &x2);
  513.     } else {
  514.         x2 = x1;
  515.     }
  516.     height = entryPtr->fontPtr->ascent + entryPtr->fontPtr->descent;
  517.     sprintf(interp->result, "%d %d %d %d", x1, 
  518.         (Tk_Height(entryPtr->tkwin) - height)/2, x2-x1, height);
  519.     } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
  520.         && (length >= 2)) {
  521.     if (argc != 3) {
  522.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  523.             argv[0], " cget option\"",
  524.             (char *) NULL);
  525.         goto error;
  526.     }
  527.     result = Tk_ConfigureValue(interp, entryPtr->tkwin, configSpecs,
  528.         (char *) entryPtr, argv[2], 0);
  529.     } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
  530.         && (length >= 2)) {
  531.     if (argc == 2) {
  532.         result = Tk_ConfigureInfo(interp, entryPtr->tkwin, configSpecs,
  533.             (char *) entryPtr, (char *) NULL, 0);
  534.     } else if (argc == 3) {
  535.         result = Tk_ConfigureInfo(interp, entryPtr->tkwin, configSpecs,
  536.             (char *) entryPtr, argv[2], 0);
  537.     } else {
  538.         result = ConfigureEntry(interp, entryPtr, argc-2, argv+2,
  539.             TK_CONFIG_ARGV_ONLY);
  540.     }
  541.     } else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
  542.     int first, last;
  543.  
  544.     if ((argc < 3) || (argc > 4)) {
  545.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  546.             argv[0], " delete firstIndex ?lastIndex?\"",
  547.             (char *) NULL);
  548.         goto error;
  549.     }
  550.     if (GetEntryIndex(interp, entryPtr, argv[2], &first) != TCL_OK) {
  551.         goto error;
  552.     }
  553.     if (argc == 3) {
  554.         last = first+1;
  555.     } else {
  556.         if (GetEntryIndex(interp, entryPtr, argv[3], &last) != TCL_OK) {
  557.         goto error;
  558.         }
  559.     }
  560.     if ((last >= first) && (entryPtr->state == tkNormalUid)) {
  561.         DeleteChars(entryPtr, first, last-first);
  562.     }
  563.     } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
  564.     if (argc != 2) {
  565.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  566.             argv[0], " get\"", (char *) NULL);
  567.         goto error;
  568.     }
  569. #ifdef STk_CODE
  570.     if (entryPtr->stringp)
  571.       STk_stringify_result(interp, entryPtr->string);
  572.     else
  573. #endif
  574.     interp->result = entryPtr->string;
  575.     } else if ((c == 'i') && (strncmp(argv[1], "icursor", length) == 0)
  576.         && (length >= 2)) {
  577.     if (argc != 3) {
  578.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  579.             argv[0], " icursor pos\"",
  580.             (char *) NULL);
  581.         goto error;
  582.     }
  583.     if (GetEntryIndex(interp, entryPtr, argv[2], &entryPtr->insertPos)
  584.         != TCL_OK) {
  585.         goto error;
  586.     }
  587.     EventuallyRedraw(entryPtr);
  588.     } else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
  589.         && (length >= 3)) {
  590.     int index;
  591.  
  592.     if (argc != 3) {
  593.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  594.             argv[0], " index string\"", (char *) NULL);
  595.         goto error;
  596.     }
  597.     if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) {
  598.         goto error;
  599.     }
  600.     sprintf(interp->result, "%d", index);
  601.     } else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)
  602.         && (length >= 3)) {
  603.     int index;
  604.  
  605.     if (argc != 4) {
  606.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  607.             argv[0], " insert index text\"",
  608.             (char *) NULL);
  609.         goto error;
  610.     }
  611.     if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) {
  612.         goto error;
  613.     }
  614.     if (entryPtr->state == tkNormalUid) {
  615.         InsertChars(entryPtr, index, argv[3]);
  616.     }
  617.     } else if ((c == 's') && (length >= 2)
  618.         && (strncmp(argv[1], "scan", length) == 0)) {
  619.     int x;
  620.  
  621.     if (argc != 4) {
  622.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  623.             argv[0], " scan mark|dragto x\"", (char *) NULL);
  624.         goto error;
  625.     }
  626.     if (Tcl_GetInt(interp, argv[3], &x) != TCL_OK) {
  627.         goto error;
  628.     }
  629.     if ((argv[2][0] == 'm')
  630.         && (strncmp(argv[2], "mark", strlen(argv[2])) == 0)) {
  631.         entryPtr->scanMarkX = x;
  632.         entryPtr->scanMarkIndex = entryPtr->leftIndex;
  633.     } else if ((argv[2][0] == 'd')
  634.         && (strncmp(argv[2], "dragto", strlen(argv[2])) == 0)) {
  635.         EntryScanTo(entryPtr, x);
  636.     } else {
  637.         Tcl_AppendResult(interp, "bad scan option \"", argv[2],
  638.             "\": must be mark or dragto", (char *) NULL);
  639.         goto error;
  640.     }
  641.     } else if ((c == 's') && (length >= 2)
  642.         && (strncmp(argv[1], "selection", length) == 0)) {
  643.     int index, index2;
  644.  
  645.     if (argc < 3) {
  646.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  647.             argv[0], " select option ?index?\"", (char *) NULL);
  648.         goto error;
  649.     }
  650.     length = strlen(argv[2]);
  651.     c = argv[2][0];
  652.     if ((c == 'c') && (strncmp(argv[2], "clear", length) == 0)) {
  653.         if (argc != 3) {
  654.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  655.             argv[0], " selection clear\"", (char *) NULL);
  656.         goto error;
  657.         }
  658.         if (entryPtr->selectFirst != -1) {
  659.         entryPtr->selectFirst = entryPtr->selectLast = -1;
  660.         EventuallyRedraw(entryPtr);
  661.         }
  662.         goto done;
  663.     } else if ((c == 'p') && (strncmp(argv[2], "present", length) == 0)) {
  664.         if (argc != 3) {
  665.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  666.             argv[0], " selection present\"", (char *) NULL);
  667.         goto error;
  668.         }
  669. #ifdef STk_CODE
  670.         interp->result = (entryPtr->selectFirst == -1) ? "#f" : "#t";
  671. #else
  672.         if (entryPtr->selectFirst == -1) {
  673.         interp->result = "0";
  674.         } else {
  675.         interp->result = "1";
  676.         }
  677. #endif
  678.         goto done;
  679.     }
  680.     if (argc >= 4) {
  681.         if (GetEntryIndex(interp, entryPtr, argv[3], &index) != TCL_OK) {
  682.         goto error;
  683.         }
  684.     }
  685.     if ((c == 'a') && (strncmp(argv[2], "adjust", length) == 0)) {
  686.         if (argc != 4) {
  687.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  688.             argv[0], " selection adjust index\"",
  689.             (char *) NULL);
  690.         goto error;
  691.         }
  692.         if (entryPtr->selectFirst >= 0) {
  693.         int half1, half2;
  694.  
  695.         half1 = (entryPtr->selectFirst + entryPtr->selectLast)/2;
  696.         half2 = (entryPtr->selectFirst + entryPtr->selectLast + 1)/2;
  697.         if (index < half1) {
  698.             entryPtr->selectAnchor = entryPtr->selectLast;
  699.         } else if (index > half2) {
  700.             entryPtr->selectAnchor = entryPtr->selectFirst;
  701.         } else {
  702.             /*
  703.              * We're at about the halfway point in the selection;
  704.              * just keep the existing anchor.
  705.              */
  706.         }
  707.         }
  708.         EntrySelectTo(entryPtr, index);
  709.     } else if ((c == 'f') && (strncmp(argv[2], "from", length) == 0)) {
  710.         if (argc != 4) {
  711.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  712.             argv[0], " selection from index\"",
  713.             (char *) NULL);
  714.         goto error;
  715.         }
  716.         entryPtr->selectAnchor = index;
  717.     } else if ((c == 'r') && (strncmp(argv[2], "range", length) == 0)) {
  718.         if (argc != 5) {
  719.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  720.             argv[0], " selection range start end\"",
  721.             (char *) NULL);
  722.         goto error;
  723.         }
  724.         if (GetEntryIndex(interp, entryPtr, argv[4], &index2) != TCL_OK) {
  725.         goto error;
  726.         }
  727.         if (index >= index2) {
  728.         entryPtr->selectFirst = entryPtr->selectLast = -1;
  729.         } else {
  730.         if ((entryPtr->selectFirst == -1)
  731.             && (entryPtr->exportSelection)) {
  732.             Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY, 
  733.                 EntryLostSelection, (ClientData) entryPtr);
  734.         }
  735.         entryPtr->selectFirst = index;
  736.         entryPtr->selectLast = index2;
  737.         }
  738.         if ((entryPtr->selectFirst == -1) && (entryPtr->exportSelection)) {
  739.         Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY,
  740.             EntryLostSelection, (ClientData) entryPtr);
  741.         }
  742.         EventuallyRedraw(entryPtr);
  743.     } else if ((c == 't') && (strncmp(argv[2], "to", length) == 0)) {
  744.         if (argc != 4) {
  745.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  746.             argv[0], " selection to index\"",
  747.             (char *) NULL);
  748.         goto error;
  749.         }
  750.         EntrySelectTo(entryPtr, index);
  751.     } else {
  752.         Tcl_AppendResult(interp, "bad selection option \"", argv[2],
  753.             "\": must be adjust, clear, from, present, range, or to",
  754.             (char *) NULL);
  755.         goto error;
  756.     }
  757.     } else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) {
  758.     int index, type, count, charsPerPage;
  759.     double fraction, first, last;
  760.  
  761.     if (argc == 2) {
  762.         EntryVisibleRange(entryPtr, &first, &last);
  763.         sprintf(interp->result, "%g %g", first, last);
  764.         goto done;
  765.     } else if (argc == 3) {
  766.         if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) {
  767.         goto error;
  768.         }
  769.     } else {
  770.         type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
  771.         index = entryPtr->leftIndex;
  772.         switch (type) {
  773.         case TK_SCROLL_ERROR:
  774.             goto error;
  775.         case TK_SCROLL_MOVETO:
  776.             index = (fraction * entryPtr->numChars) + 0.5;
  777.             break;
  778.         case TK_SCROLL_PAGES:
  779.             charsPerPage = ((Tk_Width(entryPtr->tkwin)
  780.                 - 2*entryPtr->inset) / entryPtr->avgWidth) - 2;
  781.             if (charsPerPage < 1) {
  782.             charsPerPage = 1;
  783.             }
  784.             index += charsPerPage*count;
  785.             break;
  786.         case TK_SCROLL_UNITS:
  787.             index += count;
  788.             break;
  789.         }
  790.     }
  791.     if (index >= entryPtr->numChars) {
  792.         index = entryPtr->numChars-1;
  793.     }
  794.     if (index < 0) {
  795.         index = 0;
  796.     }
  797.     entryPtr->leftIndex = index;
  798.     entryPtr->flags |= UPDATE_SCROLLBAR;
  799.     EntryComputeGeometry(entryPtr);
  800.     EventuallyRedraw(entryPtr);
  801.     } else {
  802.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  803.         "\": must be bbox, cget, configure, delete, get, ",
  804.         "icursor, index, insert, scan, selection, or xview",
  805.         (char *) NULL);
  806.     goto error;
  807.     }
  808.     done:
  809.     Tcl_Release((ClientData) entryPtr);
  810.     return result;
  811.  
  812.     error:
  813.     Tcl_Release((ClientData) entryPtr);
  814.     return TCL_ERROR;
  815. }
  816.  
  817. /*
  818.  *----------------------------------------------------------------------
  819.  *
  820.  * DestroyEntry --
  821.  *
  822.  *    This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
  823.  *    to clean up the internal structure of an entry at a safe time
  824.  *    (when no-one is using it anymore).
  825.  *
  826.  * Results:
  827.  *    None.
  828.  *
  829.  * Side effects:
  830.  *    Everything associated with the entry is freed up.
  831.  *
  832.  *----------------------------------------------------------------------
  833.  */
  834.  
  835. static void
  836. DestroyEntry(memPtr)
  837.     char *memPtr;        /* Info about entry widget. */
  838. {
  839.     register Entry *entryPtr = (Entry *) memPtr;
  840.  
  841.     /*
  842.      * Free up all the stuff that requires special handling, then
  843.      * let Tk_FreeOptions handle all the standard option-related
  844.      * stuff.
  845.      */
  846.  
  847.     ckfree(entryPtr->string);
  848.     if (entryPtr->textVarName != NULL) {
  849.     Tcl_UntraceVar(entryPtr->interp, entryPtr->textVarName,
  850.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  851.         EntryTextVarProc, (ClientData) entryPtr);
  852.     }
  853.     if (entryPtr->textGC != None) {
  854.     Tk_FreeGC(entryPtr->display, entryPtr->textGC);
  855.     }
  856.     if (entryPtr->selTextGC != None) {
  857.     Tk_FreeGC(entryPtr->display, entryPtr->selTextGC);
  858.     }
  859.     Tcl_DeleteTimerHandler(entryPtr->insertBlinkHandler);
  860.     if (entryPtr->displayString != NULL) {
  861.     ckfree(entryPtr->displayString);
  862.     }
  863.     Tk_FreeOptions(configSpecs, (char *) entryPtr, entryPtr->display, 0);
  864.     ckfree((char *) entryPtr);
  865. }
  866.  
  867. /*
  868.  *----------------------------------------------------------------------
  869.  *
  870.  * ConfigureEntry --
  871.  *
  872.  *    This procedure is called to process an argv/argc list, plus
  873.  *    the Tk option database, in order to configure (or reconfigure)
  874.  *    an entry widget.
  875.  *
  876.  * Results:
  877.  *    The return value is a standard Tcl result.  If TCL_ERROR is
  878.  *    returned, then interp->result contains an error message.
  879.  *
  880.  * Side effects:
  881.  *    Configuration information, such as colors, border width,
  882.  *    etc. get set for entryPtr;  old resources get freed,
  883.  *    if there were any.
  884.  *
  885.  *----------------------------------------------------------------------
  886.  */
  887.  
  888. static int
  889. ConfigureEntry(interp, entryPtr, argc, argv, flags)
  890.     Tcl_Interp *interp;        /* Used for error reporting. */
  891.     register Entry *entryPtr;    /* Information about widget;  may or may
  892.                  * not already have values for some fields. */
  893.     int argc;            /* Number of valid entries in argv. */
  894.     char **argv;        /* Arguments. */
  895.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  896. {
  897.     XGCValues gcValues;
  898.     GC new;
  899.     int oldExport;
  900.  
  901.     /*
  902.      * Eliminate any existing trace on a variable monitored by the entry.
  903.      */
  904.  
  905.     if (entryPtr->textVarName != NULL) {
  906.     Tcl_UntraceVar(interp, entryPtr->textVarName, 
  907.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  908.         EntryTextVarProc, (ClientData) entryPtr);
  909.     }
  910.  
  911.     oldExport = entryPtr->exportSelection;
  912.     if (Tk_ConfigureWidget(interp, entryPtr->tkwin, configSpecs,
  913.         argc, argv, (char *) entryPtr, flags) != TCL_OK) {
  914.     return TCL_ERROR;
  915.     }
  916.  
  917.     /*
  918.      * If the entry is tied to the value of a variable, then set up
  919.      * a trace on the variable's value, create the variable if it doesn't
  920.      * exist, and set the entry's value from the variable's value.
  921.      */
  922.  
  923.     if (entryPtr->textVarName != NULL) {
  924.     char *value;
  925.  
  926.     value = Tcl_GetVar(interp, entryPtr->textVarName, TCL_GLOBAL_ONLY);
  927.     if (value == NULL) {
  928.         Tcl_SetVar(interp, entryPtr->textVarName, entryPtr->string,
  929. #ifdef STk_CODE
  930.                (entryPtr->stringp? STk_STRINGIFY: 0) |
  931. #endif
  932.             TCL_GLOBAL_ONLY);
  933.     } else {
  934.         EntrySetValue(entryPtr, value);
  935.     }
  936.     Tcl_TraceVar(interp, entryPtr->textVarName,
  937.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  938.         EntryTextVarProc, (ClientData) entryPtr);
  939.     }
  940.  
  941.     /*
  942.      * A few other options also need special processing, such as parsing
  943.      * the geometry and setting the background from a 3-D border.
  944.      */
  945.  
  946.     if ((entryPtr->state != tkNormalUid)
  947.         && (entryPtr->state != tkDisabledUid)) {
  948.     Tcl_AppendResult(interp, "bad state value \"", entryPtr->state,
  949.         "\": must be normal or disabled", (char *) NULL);
  950.     entryPtr->state = tkNormalUid;
  951.     return TCL_ERROR;
  952.     }
  953.  
  954.     Tk_SetBackgroundFromBorder(entryPtr->tkwin, entryPtr->normalBorder);
  955.  
  956.     gcValues.foreground = entryPtr->fgColorPtr->pixel;
  957.     gcValues.font = entryPtr->fontPtr->fid;
  958.     gcValues.graphics_exposures = False;
  959.     new = Tk_GetGC(entryPtr->tkwin, GCForeground|GCFont|GCGraphicsExposures,
  960.         &gcValues);
  961.     if (entryPtr->textGC != None) {
  962.     Tk_FreeGC(entryPtr->display, entryPtr->textGC);
  963.     }
  964.     entryPtr->textGC = new;
  965.  
  966.     gcValues.foreground = entryPtr->selFgColorPtr->pixel;
  967.     gcValues.font = entryPtr->fontPtr->fid;
  968.     new = Tk_GetGC(entryPtr->tkwin, GCForeground|GCFont, &gcValues);
  969.     if (entryPtr->selTextGC != None) {
  970.     Tk_FreeGC(entryPtr->display, entryPtr->selTextGC);
  971.     }
  972.     entryPtr->selTextGC = new;
  973.  
  974.     if (entryPtr->insertWidth <= 0) {
  975.     entryPtr->insertWidth = 2;
  976.     }
  977.     if (entryPtr->insertBorderWidth > entryPtr->insertWidth/2) {
  978.     entryPtr->insertBorderWidth = entryPtr->insertWidth/2;
  979.     }
  980.  
  981.     /*
  982.      * Restart the cursor timing sequence in case the on-time or off-time
  983.      * just changed.
  984.      */
  985.  
  986.     if (entryPtr->flags & GOT_FOCUS) {
  987.     EntryFocusProc(entryPtr, 1);
  988.     }
  989.  
  990.     /*
  991.      * Claim the selection if we've suddenly started exporting it.
  992.      */
  993.  
  994.     if (entryPtr->exportSelection && (!oldExport)
  995.         && (entryPtr->selectFirst != -1)) {
  996.     Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY, EntryLostSelection,
  997.         (ClientData) entryPtr);
  998.     }
  999.  
  1000.     /*
  1001.      * Recompute the window's geometry and arrange for it to be
  1002.      * redisplayed.
  1003.      */
  1004.  
  1005.     Tk_SetInternalBorder(entryPtr->tkwin,
  1006.         entryPtr->borderWidth + entryPtr->highlightWidth);
  1007.     if (entryPtr->highlightWidth <= 0) {
  1008.     entryPtr->highlightWidth = 0;
  1009.     }
  1010.     entryPtr->inset = entryPtr->highlightWidth + entryPtr->borderWidth + XPAD;
  1011.     entryPtr->avgWidth = XTextWidth(entryPtr->fontPtr, "0", 1);
  1012.     EntryComputeGeometry(entryPtr);
  1013.     entryPtr->flags |= UPDATE_SCROLLBAR;
  1014.     EventuallyRedraw(entryPtr);
  1015.     return TCL_OK;
  1016. }
  1017.  
  1018. /*
  1019.  *--------------------------------------------------------------
  1020.  *
  1021.  * DisplayEntry --
  1022.  *
  1023.  *    This procedure redraws the contents of an entry window.
  1024.  *
  1025.  * Results:
  1026.  *    None.
  1027.  *
  1028.  * Side effects:
  1029.  *    Information appears on the screen.
  1030.  *
  1031.  *--------------------------------------------------------------
  1032.  */
  1033.  
  1034. static void
  1035. DisplayEntry(clientData)
  1036.     ClientData clientData;    /* Information about window. */
  1037. {
  1038.     register Entry *entryPtr = (Entry *) clientData;
  1039.     register Tk_Window tkwin = entryPtr->tkwin;
  1040.     int baseY, selStartX, selEndX, index, cursorX;
  1041.     int xBound, count;
  1042.     Pixmap pixmap;
  1043.     char *displayString;
  1044.  
  1045.     entryPtr->flags &= ~REDRAW_PENDING;
  1046.     if ((entryPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
  1047.     return;
  1048.     }
  1049.  
  1050.     /*
  1051.      * Update the scrollbar if that's needed.
  1052.      */
  1053.  
  1054.     if (entryPtr->flags & UPDATE_SCROLLBAR) {
  1055.     EntryUpdateScrollbar(entryPtr);
  1056.     }
  1057.  
  1058.     /*
  1059.      * In order to avoid screen flashes, this procedure redraws the
  1060.      * textual area of the entry into off-screen memory, then copies
  1061.      * it back on-screen in a single operation.  This means there's
  1062.      * no point in time where the on-screen image has been cleared.
  1063.      */
  1064.  
  1065.     pixmap = Tk_GetPixmap(entryPtr->display, Tk_WindowId(tkwin),
  1066.         Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
  1067.  
  1068.     /*
  1069.      * Compute x-coordinate of the pixel just after last visible
  1070.      * one, plus vertical position of baseline of text.
  1071.      */
  1072.  
  1073.     xBound = Tk_Width(tkwin) - entryPtr->inset;
  1074.     baseY = (Tk_Height(tkwin) + entryPtr->fontPtr->ascent
  1075.         - entryPtr->fontPtr->descent)/2;
  1076.  
  1077.     /*
  1078.      * Draw the background in three layers.  From bottom to top the
  1079.      * layers are:  normal background, selection background, and
  1080.      * insertion cursor background.
  1081.      */
  1082.  
  1083.     Tk_Fill3DRectangle(tkwin, pixmap, entryPtr->normalBorder,
  1084.         0, 0, Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
  1085.     if (entryPtr->displayString == NULL) {
  1086.     displayString = entryPtr->string;
  1087.     } else {
  1088.     displayString = entryPtr->displayString;
  1089.     }
  1090.     if (entryPtr->selectLast > entryPtr->leftIndex) {
  1091.     if (entryPtr->selectFirst <= entryPtr->leftIndex) {
  1092.         selStartX = entryPtr->leftX;
  1093.         index = entryPtr->leftIndex;
  1094.     } else {
  1095.         (void) TkMeasureChars(entryPtr->fontPtr,
  1096.             displayString + entryPtr->leftIndex,
  1097.             entryPtr->selectFirst - entryPtr->leftIndex,
  1098.             entryPtr->leftX, xBound, entryPtr->tabOrigin,
  1099.             TK_PARTIAL_OK|TK_NEWLINES_NOT_SPECIAL, &selStartX);
  1100.         index = entryPtr->selectFirst;
  1101.     }
  1102.     if ((selStartX - entryPtr->selBorderWidth) < xBound) {
  1103.         (void) TkMeasureChars(entryPtr->fontPtr,
  1104.             displayString + index, entryPtr->selectLast - index,
  1105.             selStartX, xBound, entryPtr->tabOrigin,
  1106.             TK_PARTIAL_OK|TK_NEWLINES_NOT_SPECIAL, &selEndX);
  1107.         Tk_Fill3DRectangle(tkwin, pixmap, entryPtr->selBorder,
  1108.             selStartX - entryPtr->selBorderWidth,
  1109.             baseY - entryPtr->fontPtr->ascent
  1110.                 - entryPtr->selBorderWidth,
  1111.             (selEndX - selStartX) + 2*entryPtr->selBorderWidth,
  1112.             entryPtr->fontPtr->ascent + entryPtr->fontPtr->descent
  1113.                 + 2*entryPtr->selBorderWidth,
  1114.             entryPtr->selBorderWidth, TK_RELIEF_RAISED);
  1115.     } else {
  1116.         selEndX = xBound;
  1117.     }
  1118.     }
  1119.  
  1120.     /*
  1121.      * Draw a special background for the insertion cursor, overriding
  1122.      * even the selection background.  As a special hack to keep the
  1123.      * cursor visible when the insertion cursor color is the same as
  1124.      * the color for selected text (e.g., on mono displays), write
  1125.      * background in the cursor area (instead of nothing) when the
  1126.      * cursor isn't on.  Otherwise the selection would hide the cursor.
  1127.      */
  1128.  
  1129.     if ((entryPtr->insertPos >= entryPtr->leftIndex)
  1130.         && (entryPtr->state == tkNormalUid)
  1131.         && (entryPtr->flags & GOT_FOCUS)) {
  1132.     (void) TkMeasureChars(entryPtr->fontPtr,
  1133.         displayString + entryPtr->leftIndex,
  1134.         entryPtr->insertPos - entryPtr->leftIndex, entryPtr->leftX,
  1135.         xBound + entryPtr->insertWidth, entryPtr->tabOrigin,
  1136.         TK_PARTIAL_OK|TK_NEWLINES_NOT_SPECIAL, &cursorX);
  1137.     cursorX -= (entryPtr->insertWidth)/2;
  1138.     if (cursorX < xBound) {
  1139.         if (entryPtr->flags & CURSOR_ON) {
  1140.         Tk_Fill3DRectangle(tkwin, pixmap, entryPtr->insertBorder,
  1141.             cursorX, baseY - entryPtr->fontPtr->ascent,
  1142.             entryPtr->insertWidth,
  1143.             entryPtr->fontPtr->ascent + entryPtr->fontPtr->descent,
  1144.             entryPtr->insertBorderWidth, TK_RELIEF_RAISED);
  1145.         } else if (entryPtr->insertBorder == entryPtr->selBorder) {
  1146.         Tk_Fill3DRectangle(tkwin, pixmap, entryPtr->normalBorder,
  1147.             cursorX, baseY - entryPtr->fontPtr->ascent,
  1148.             entryPtr->insertWidth,
  1149.             entryPtr->fontPtr->ascent + entryPtr->fontPtr->descent,
  1150.             0, TK_RELIEF_FLAT);
  1151.         }
  1152.     }
  1153.     }
  1154.  
  1155.     /*
  1156.      * Draw the text in three pieces:  first the piece to the left of
  1157.      * the selection, then the selection, then the piece to the right
  1158.      * of the selection.
  1159.      */
  1160.  
  1161.     if (entryPtr->selectLast <= entryPtr->leftIndex) {
  1162.     TkDisplayChars(entryPtr->display, pixmap, entryPtr->textGC,
  1163.         entryPtr->fontPtr, displayString + entryPtr->leftIndex,
  1164.         entryPtr->numChars - entryPtr->leftIndex, entryPtr->leftX,
  1165.         baseY, entryPtr->tabOrigin, TK_NEWLINES_NOT_SPECIAL);
  1166.     } else {
  1167.     count = entryPtr->selectFirst - entryPtr->leftIndex;
  1168.     if (count > 0) {
  1169.         TkDisplayChars(entryPtr->display, pixmap, entryPtr->textGC,
  1170.             entryPtr->fontPtr, displayString + entryPtr->leftIndex,
  1171.             count, entryPtr->leftX, baseY, entryPtr->tabOrigin,
  1172.             TK_NEWLINES_NOT_SPECIAL);
  1173.         index = entryPtr->selectFirst;
  1174.     } else {
  1175.         index = entryPtr->leftIndex;
  1176.     }
  1177.     count = entryPtr->selectLast - index;
  1178.     if ((selStartX < xBound) && (count > 0)) {
  1179.         TkDisplayChars(entryPtr->display, pixmap, entryPtr->selTextGC,
  1180.             entryPtr->fontPtr, displayString + index, count,
  1181.             selStartX, baseY, entryPtr->tabOrigin,
  1182.             TK_NEWLINES_NOT_SPECIAL);
  1183.     }
  1184.     count = entryPtr->numChars - entryPtr->selectLast;
  1185.     if ((selEndX < xBound) && (count > 0)) {
  1186.         TkDisplayChars(entryPtr->display, pixmap, entryPtr->textGC,
  1187.             entryPtr->fontPtr,
  1188.             displayString + entryPtr->selectLast,
  1189.             count, selEndX, baseY, entryPtr->tabOrigin,
  1190.             TK_NEWLINES_NOT_SPECIAL);
  1191.     }
  1192.     }
  1193.  
  1194.     /*
  1195.      * Draw the border and focus highlight last, so they will overwrite
  1196.      * any text that extends past the viewable part of the window.
  1197.      */
  1198.  
  1199.     if (entryPtr->relief != TK_RELIEF_FLAT) {
  1200.     Tk_Draw3DRectangle(tkwin, pixmap, entryPtr->normalBorder,
  1201.         entryPtr->highlightWidth, entryPtr->highlightWidth,
  1202.         Tk_Width(tkwin) - 2*entryPtr->highlightWidth,
  1203.         Tk_Height(tkwin) - 2*entryPtr->highlightWidth,
  1204.         entryPtr->borderWidth, entryPtr->relief);
  1205.     }
  1206.     if (entryPtr->highlightWidth != 0) {
  1207.     GC gc;
  1208.  
  1209.     if (entryPtr->flags & GOT_FOCUS) {
  1210.         gc = Tk_GCForColor(entryPtr->highlightColorPtr, pixmap);
  1211.     } else {
  1212.         gc = Tk_GCForColor(entryPtr->highlightBgColorPtr, pixmap);
  1213.     }
  1214.     Tk_DrawFocusHighlight(tkwin, gc, entryPtr->highlightWidth, pixmap);
  1215.     }
  1216.  
  1217.     /*
  1218.      * Everything's been redisplayed;  now copy the pixmap onto the screen
  1219.      * and free up the pixmap.
  1220.      */
  1221.  
  1222.     XCopyArea(entryPtr->display, pixmap, Tk_WindowId(tkwin), entryPtr->textGC,
  1223.         0, 0, (unsigned) Tk_Width(tkwin), (unsigned) Tk_Height(tkwin),
  1224.         0, 0);
  1225.     Tk_FreePixmap(entryPtr->display, pixmap);
  1226.     entryPtr->flags &= ~BORDER_NEEDED;
  1227. }
  1228.  
  1229. /*
  1230.  *----------------------------------------------------------------------
  1231.  *
  1232.  * EntryComputeGeometry --
  1233.  *
  1234.  *    This procedure is invoked to recompute information about where
  1235.  *    in its window an entry's string will be displayed.  It also
  1236.  *    computes the requested size for the window.
  1237.  *
  1238.  * Results:
  1239.  *    None.
  1240.  *
  1241.  * Side effects:
  1242.  *    The leftX and tabOrigin fields are recomputed for entryPtr,
  1243.  *    and leftIndex may be adjusted.  Tk_GeometryRequest is called
  1244.  *    to register the desired dimensions for the window.
  1245.  *
  1246.  *----------------------------------------------------------------------
  1247.  */
  1248.  
  1249. static void
  1250. EntryComputeGeometry(entryPtr)
  1251.     Entry *entryPtr;            /* Widget record for entry. */
  1252. {
  1253.     int totalLength, overflow, maxOffScreen, rightX;
  1254.     int fontHeight, height, width, i;
  1255.     char *p, *displayString;
  1256.  
  1257.     /*
  1258.      * If we're displaying a special character instead of the value of
  1259.      * the entry, recompute the displayString.
  1260.      */
  1261.  
  1262.     if (entryPtr->displayString != NULL) {
  1263.     ckfree(entryPtr->displayString);
  1264.     entryPtr->displayString = NULL;
  1265.     }
  1266.     if (entryPtr->showChar != NULL) {
  1267.     entryPtr->displayString = (char *) ckalloc((unsigned)
  1268.         (entryPtr->numChars + 1));
  1269.     for (p = entryPtr->displayString, i = entryPtr->numChars; i > 0;
  1270.         i--, p++) {
  1271.         *p = entryPtr->showChar[0];
  1272.     }
  1273.     *p = 0;
  1274.     displayString = entryPtr->displayString;
  1275.     } else {
  1276.     displayString = entryPtr->string;
  1277.     }
  1278.  
  1279.     /*
  1280.      * Recompute where the leftmost character on the display will
  1281.      * be drawn (entryPtr->leftX) and adjust leftIndex if necessary
  1282.      * so that we don't let characters hang off the edge of the
  1283.      * window unless the entire window is full.
  1284.      */
  1285.  
  1286.     TkMeasureChars(entryPtr->fontPtr, displayString, entryPtr->numChars,
  1287.         0, INT_MAX, 0, TK_NEWLINES_NOT_SPECIAL, &totalLength);
  1288.     overflow = totalLength - (Tk_Width(entryPtr->tkwin) - 2*entryPtr->inset);
  1289.     if (overflow <= 0) {
  1290.     entryPtr->leftIndex = 0;
  1291.     if (entryPtr->justify == TK_JUSTIFY_LEFT) {
  1292.         entryPtr->leftX = entryPtr->inset;
  1293.     } else if (entryPtr->justify == TK_JUSTIFY_RIGHT) {
  1294.         entryPtr->leftX = Tk_Width(entryPtr->tkwin) - entryPtr->inset
  1295.             - totalLength;
  1296.     } else {
  1297.         entryPtr->leftX = (Tk_Width(entryPtr->tkwin) - totalLength)/2;
  1298.     }
  1299.     entryPtr->tabOrigin = entryPtr->leftX;
  1300.     } else {
  1301.     /*
  1302.      * The whole string can't fit in the window.  Compute the
  1303.      * maximum number of characters that may be off-screen to
  1304.      * the left without leaving empty space on the right of the
  1305.      * window, then don't let leftIndex be any greater than that.
  1306.      */
  1307.  
  1308.     maxOffScreen = TkMeasureChars(entryPtr->fontPtr, displayString,
  1309.         entryPtr->numChars, 0, overflow, 0,
  1310.         TK_NEWLINES_NOT_SPECIAL|TK_PARTIAL_OK, &rightX);
  1311.     if (rightX < overflow) {
  1312.         maxOffScreen += 1;
  1313.     }
  1314.     if (entryPtr->leftIndex > maxOffScreen) {
  1315.         entryPtr->leftIndex = maxOffScreen;
  1316.     }
  1317.     TkMeasureChars(entryPtr->fontPtr, displayString,
  1318.         entryPtr->leftIndex, 0, INT_MAX, 0,
  1319.         TK_NEWLINES_NOT_SPECIAL|TK_PARTIAL_OK, &rightX);
  1320.     entryPtr->leftX = entryPtr->inset;
  1321.     entryPtr->tabOrigin = entryPtr->leftX - rightX;
  1322.     }
  1323.  
  1324.     fontHeight = entryPtr->fontPtr->ascent + entryPtr->fontPtr->descent;
  1325.     height = fontHeight + 2*entryPtr->inset + 2*(YPAD-XPAD);
  1326.     if (entryPtr->prefWidth > 0) {
  1327.     width = entryPtr->prefWidth*entryPtr->avgWidth + 2*entryPtr->inset;
  1328.     } else {
  1329.     if (totalLength == 0) {
  1330.         width = entryPtr->avgWidth + 2*entryPtr->inset;
  1331.     } else {
  1332.         width = totalLength + 2*entryPtr->inset;
  1333.     }
  1334.     }
  1335.     Tk_GeometryRequest(entryPtr->tkwin, width, height);
  1336. }
  1337.  
  1338. /*
  1339.  *----------------------------------------------------------------------
  1340.  *
  1341.  * InsertChars --
  1342.  *
  1343.  *    Add new characters to an entry widget.
  1344.  *
  1345.  * Results:
  1346.  *    None.
  1347.  *
  1348.  * Side effects:
  1349.  *    New information gets added to entryPtr;  it will be redisplayed
  1350.  *    soon, but not necessarily immediately.
  1351.  *
  1352.  *----------------------------------------------------------------------
  1353.  */
  1354.  
  1355. static void
  1356. InsertChars(entryPtr, index, string)
  1357.     register Entry *entryPtr;    /* Entry that is to get the new
  1358.                  * elements. */
  1359.     int index;            /* Add the new elements before this
  1360.                  * element. */
  1361.     char *string;        /* New characters to add (NULL-terminated
  1362.                  * string). */
  1363. {
  1364.     int length;
  1365.     char *new;
  1366.  
  1367.     length = strlen(string);
  1368.     if (length == 0) {
  1369.     return;
  1370.     }
  1371.     new = (char *) ckalloc((unsigned) (entryPtr->numChars + length + 1));
  1372.     strncpy(new, entryPtr->string, (size_t) index);
  1373.     strcpy(new+index, string);
  1374.     strcpy(new+index+length, entryPtr->string+index);
  1375.     ckfree(entryPtr->string);
  1376.     entryPtr->string = new;
  1377.     entryPtr->numChars += length;
  1378.  
  1379.     /*
  1380.      * Inserting characters invalidates all indexes into the string.
  1381.      * Touch up the indexes so that they still refer to the same
  1382.      * characters (at new positions).  When updating the selection
  1383.      * end-points, don't include the new text in the selection unless
  1384.      * it was completely surrounded by the selection.
  1385.      */
  1386.  
  1387.     if (entryPtr->selectFirst >= index) {
  1388.     entryPtr->selectFirst += length;
  1389.     }
  1390.     if (entryPtr->selectLast > index) {
  1391.     entryPtr->selectLast += length;
  1392.     }
  1393.     if ((entryPtr->selectAnchor > index) || (entryPtr->selectFirst >= index)) {
  1394.     entryPtr->selectAnchor += length;
  1395.     }
  1396.     if (entryPtr->leftIndex > index) {
  1397.     entryPtr->leftIndex += length;
  1398.     }
  1399.     if (entryPtr->insertPos >= index) {
  1400.     entryPtr->insertPos += length;
  1401.     }
  1402.     EntryValueChanged(entryPtr);
  1403. }
  1404.  
  1405. /*
  1406.  *----------------------------------------------------------------------
  1407.  *
  1408.  * DeleteChars --
  1409.  *
  1410.  *    Remove one or more characters from an entry widget.
  1411.  *
  1412.  * Results:
  1413.  *    None.
  1414.  *
  1415.  * Side effects:
  1416.  *    Memory gets freed, the entry gets modified and (eventually)
  1417.  *    redisplayed.
  1418.  *
  1419.  *----------------------------------------------------------------------
  1420.  */
  1421.  
  1422. static void
  1423. DeleteChars(entryPtr, index, count)
  1424.     register Entry *entryPtr;    /* Entry widget to modify. */
  1425.     int index;            /* Index of first character to delete. */
  1426.     int count;            /* How many characters to delete. */
  1427. {
  1428.     char *new;
  1429.  
  1430.     if ((index + count) > entryPtr->numChars) {
  1431.     count = entryPtr->numChars - index;
  1432.     }
  1433.     if (count <= 0) {
  1434.     return;
  1435.     }
  1436.  
  1437.     new = (char *) ckalloc((unsigned) (entryPtr->numChars + 1 - count));
  1438.     strncpy(new, entryPtr->string, (size_t) index);
  1439.     strcpy(new+index, entryPtr->string+index+count);
  1440.     ckfree(entryPtr->string);
  1441.     entryPtr->string = new;
  1442.     entryPtr->numChars -= count;
  1443.  
  1444.     /*
  1445.      * Deleting characters results in the remaining characters being
  1446.      * renumbered.  Update the various indexes into the string to reflect
  1447.      * this change.
  1448.      */
  1449.  
  1450.     if (entryPtr->selectFirst >= index) {
  1451.     if (entryPtr->selectFirst >= (index+count)) {
  1452.         entryPtr->selectFirst -= count;
  1453.     } else {
  1454.         entryPtr->selectFirst = index;
  1455.     }
  1456.     }
  1457.     if (entryPtr->selectLast >= index) {
  1458.     if (entryPtr->selectLast >= (index+count)) {
  1459.         entryPtr->selectLast -= count;
  1460.     } else {
  1461.         entryPtr->selectLast = index;
  1462.     }
  1463.     }
  1464.     if (entryPtr->selectLast <= entryPtr->selectFirst) {
  1465.     entryPtr->selectFirst = entryPtr->selectLast = -1;
  1466.     }
  1467.     if (entryPtr->selectAnchor >= index) {
  1468.     if (entryPtr->selectAnchor >= (index+count)) {
  1469.         entryPtr->selectAnchor -= count;
  1470.     } else {
  1471.         entryPtr->selectAnchor = index;
  1472.     }
  1473.     }
  1474.     if (entryPtr->leftIndex > index) {
  1475.     if (entryPtr->leftIndex >= (index+count)) {
  1476.         entryPtr->leftIndex -= count;
  1477.     } else {
  1478.         entryPtr->leftIndex = index;
  1479.     }
  1480.     }
  1481.     if (entryPtr->insertPos >= index) {
  1482.     if (entryPtr->insertPos >= (index+count)) {
  1483.         entryPtr->insertPos -= count;
  1484.     } else {
  1485.         entryPtr->insertPos = index;
  1486.     }
  1487.     }
  1488.     EntryValueChanged(entryPtr);
  1489. }
  1490.  
  1491. /*
  1492.  *----------------------------------------------------------------------
  1493.  *
  1494.  * EntryValueChanged --
  1495.  *
  1496.  *    This procedure is invoked when characters are inserted into
  1497.  *    an entry or deleted from it.  It updates the entry's associated
  1498.  *    variable, if there is one, and does other bookkeeping such
  1499.  *    as arranging for redisplay.
  1500.  *
  1501.  * Results:
  1502.  *    None.
  1503.  *
  1504.  * Side effects:
  1505.  *    None.
  1506.  *
  1507.  *----------------------------------------------------------------------
  1508.  */
  1509.  
  1510. static void
  1511. EntryValueChanged(entryPtr)
  1512.     Entry *entryPtr;        /* Entry whose value just changed. */
  1513. {
  1514.     char *newValue;
  1515.  
  1516.     if (entryPtr->textVarName == NULL) {
  1517.     newValue = NULL;
  1518.     } else {
  1519.     newValue = Tcl_SetVar(entryPtr->interp, entryPtr->textVarName,
  1520. #ifdef STk_CODE
  1521.         entryPtr->string, 
  1522.             (entryPtr->stringp ? STk_STRINGIFY : 0) | TCL_GLOBAL_ONLY);
  1523. #else
  1524.         entryPtr->string, TCL_GLOBAL_ONLY);
  1525. #endif
  1526.     }
  1527.  
  1528.     if ((newValue != NULL) && (strcmp(newValue, entryPtr->string) != 0)) {
  1529.     /*
  1530.      * The value of the variable is different than what we asked for.
  1531.      * This means that a trace on the variable modified it.  In this
  1532.      * case our trace procedure wasn't invoked since the modification
  1533.      * came while a trace was already active on the variable.  So,
  1534.      * update our value to reflect the variable's latest value.
  1535.      */
  1536.  
  1537.     EntrySetValue(entryPtr, newValue);
  1538.     } else {
  1539.     /*
  1540.      * Arrange for redisplay.
  1541.      */
  1542.  
  1543.     entryPtr->flags |= UPDATE_SCROLLBAR;
  1544.     EntryComputeGeometry(entryPtr);
  1545.     EventuallyRedraw(entryPtr);
  1546.     }
  1547. }
  1548.  
  1549. /*
  1550.  *----------------------------------------------------------------------
  1551.  *
  1552.  * EntrySetValue --
  1553.  *
  1554.  *    Replace the contents of a text entry with a given value.  This
  1555.  *    procedure is invoked when updating the entry from the entry's
  1556.  *    associated variable.
  1557.  *
  1558.  * Results:
  1559.  *    None.
  1560.  *
  1561.  * Side effects:
  1562.  *    The string displayed in the entry will change.  The selection,
  1563.  *    insertion point, and view may have to be adjusted to keep them
  1564.  *    within the bounds of the new string.  Note: this procedure does
  1565.  *    *not* update the entry's associated variable, since that could
  1566.  *    result in an infinite loop.
  1567.  *
  1568.  *----------------------------------------------------------------------
  1569.  */
  1570.  
  1571. static void
  1572. EntrySetValue(entryPtr, value)
  1573.     register Entry *entryPtr;        /* Entry whose value is to be
  1574.                      * changed. */
  1575.     char *value;            /* New text to display in entry. */
  1576. {
  1577.     ckfree(entryPtr->string);
  1578.     entryPtr->numChars = strlen(value);
  1579.     entryPtr->string = (char *) ckalloc((unsigned) (entryPtr->numChars + 1));
  1580.     strcpy(entryPtr->string, value);
  1581.     if (entryPtr->selectFirst != -1) {
  1582.     if (entryPtr->selectFirst >= entryPtr->numChars) {
  1583.         entryPtr->selectFirst = entryPtr->selectLast = -1;
  1584.     } else if (entryPtr->selectLast > entryPtr->numChars) {
  1585.         entryPtr->selectLast = entryPtr->numChars;
  1586.     }
  1587.     }
  1588.     if (entryPtr->leftIndex >= entryPtr->numChars) {
  1589.     entryPtr->leftIndex = entryPtr->numChars-1;
  1590.     }
  1591.     if (entryPtr->insertPos > entryPtr->numChars) {
  1592.     entryPtr->insertPos = entryPtr->numChars;
  1593.     }
  1594.  
  1595.     entryPtr->flags |= UPDATE_SCROLLBAR;
  1596.     EntryComputeGeometry(entryPtr);
  1597.     EventuallyRedraw(entryPtr);
  1598. }
  1599.  
  1600. /*
  1601.  *--------------------------------------------------------------
  1602.  *
  1603.  * EntryEventProc --
  1604.  *
  1605.  *    This procedure is invoked by the Tk dispatcher for various
  1606.  *    events on entryes.
  1607.  *
  1608.  * Results:
  1609.  *    None.
  1610.  *
  1611.  * Side effects:
  1612.  *    When the window gets deleted, internal structures get
  1613.  *    cleaned up.  When it gets exposed, it is redisplayed.
  1614.  *
  1615.  *--------------------------------------------------------------
  1616.  */
  1617.  
  1618. static void
  1619. EntryEventProc(clientData, eventPtr)
  1620.     ClientData clientData;    /* Information about window. */
  1621.     XEvent *eventPtr;        /* Information about event. */
  1622. {
  1623.     Entry *entryPtr = (Entry *) clientData;
  1624.     if (eventPtr->type == Expose) {
  1625.     EventuallyRedraw(entryPtr);
  1626.     entryPtr->flags |= BORDER_NEEDED;
  1627.     } else if (eventPtr->type == DestroyNotify) {
  1628.     if (entryPtr->tkwin != NULL) {
  1629.         entryPtr->tkwin = NULL;
  1630.         Tcl_DeleteCommand(entryPtr->interp,
  1631.             Tcl_GetCommandName(entryPtr->interp, entryPtr->widgetCmd));
  1632.     }
  1633.     if (entryPtr->flags & REDRAW_PENDING) {
  1634.         Tcl_CancelIdleCall(DisplayEntry, (ClientData) entryPtr);
  1635.     }
  1636.     Tcl_EventuallyFree((ClientData) entryPtr, DestroyEntry);
  1637.     } else if (eventPtr->type == ConfigureNotify) {
  1638.     Tcl_Preserve((ClientData) entryPtr);
  1639.     entryPtr->flags |= UPDATE_SCROLLBAR;
  1640.     EntryComputeGeometry(entryPtr);
  1641.     EventuallyRedraw(entryPtr);
  1642.     Tcl_Release((ClientData) entryPtr);
  1643.     } else if (eventPtr->type == FocusIn) {
  1644.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1645.         EntryFocusProc(entryPtr, 1);
  1646.     }
  1647.     } else if (eventPtr->type == FocusOut) {
  1648.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1649.         EntryFocusProc(entryPtr, 0);
  1650.     }
  1651.     }
  1652. }
  1653.  
  1654. /*
  1655.  *----------------------------------------------------------------------
  1656.  *
  1657.  * EntryCmdDeletedProc --
  1658.  *
  1659.  *    This procedure is invoked when a widget command is deleted.  If
  1660.  *    the widget isn't already in the process of being destroyed,
  1661.  *    this command destroys it.
  1662.  *
  1663.  * Results:
  1664.  *    None.
  1665.  *
  1666.  * Side effects:
  1667.  *    The widget is destroyed.
  1668.  *
  1669.  *----------------------------------------------------------------------
  1670.  */
  1671.  
  1672. static void
  1673. EntryCmdDeletedProc(clientData)
  1674.     ClientData clientData;    /* Pointer to widget record for widget. */
  1675. {
  1676.     Entry *entryPtr = (Entry *) clientData;
  1677.     Tk_Window tkwin = entryPtr->tkwin;
  1678.  
  1679.     /*
  1680.      * This procedure could be invoked either because the window was
  1681.      * destroyed and the command was then deleted (in which case tkwin
  1682.      * is NULL) or because the command was deleted, and then this procedure
  1683.      * destroys the widget.
  1684.      */
  1685.  
  1686.     if (tkwin != NULL) {
  1687.     entryPtr->tkwin = NULL;
  1688.     Tk_DestroyWindow(tkwin);
  1689.     }
  1690. }
  1691.  
  1692. /*
  1693.  *--------------------------------------------------------------
  1694.  *
  1695.  * GetEntryIndex --
  1696.  *
  1697.  *    Parse an index into an entry and return either its value
  1698.  *    or an error.
  1699.  *
  1700.  * Results:
  1701.  *    A standard Tcl result.  If all went well, then *indexPtr is
  1702.  *    filled in with the index (into entryPtr) corresponding to
  1703.  *    string.  The index value is guaranteed to lie between 0 and
  1704.  *    the number of characters in the string, inclusive.  If an
  1705.  *    error occurs then an error message is left in interp->result.
  1706.  *
  1707.  * Side effects:
  1708.  *    None.
  1709.  *
  1710.  *--------------------------------------------------------------
  1711.  */
  1712.  
  1713. static int
  1714. GetEntryIndex(interp, entryPtr, string, indexPtr)
  1715.     Tcl_Interp *interp;        /* For error messages. */
  1716.     Entry *entryPtr;        /* Entry for which the index is being
  1717.                  * specified. */
  1718.     char *string;        /* Specifies character in entryPtr. */
  1719.     int *indexPtr;        /* Where to store converted index. */
  1720. {
  1721.     size_t length;
  1722.  
  1723.     length = strlen(string);
  1724.  
  1725.     if (string[0] == 'a') {
  1726.     if (strncmp(string, "anchor", length) == 0) {
  1727.         *indexPtr = entryPtr->selectAnchor;
  1728.     } else {
  1729.         badIndex:
  1730.  
  1731.         /*
  1732.          * Some of the paths here leave messages in interp->result,
  1733.          * so we have to clear it out before storing our own message.
  1734.          */
  1735.  
  1736.         Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
  1737.         Tcl_AppendResult(interp, "bad entry index \"", string,
  1738.             "\"", (char *) NULL);
  1739.         return TCL_ERROR;
  1740.     }
  1741.     } else if (string[0] == 'e') {
  1742.     if (strncmp(string, "end", length) == 0) {
  1743.         *indexPtr = entryPtr->numChars;
  1744.     } else {
  1745.         goto badIndex;
  1746.     }
  1747.     } else if (string[0] == 'i') {
  1748.     if (strncmp(string, "insert", length) == 0) {
  1749.         *indexPtr = entryPtr->insertPos;
  1750.     } else {
  1751.         goto badIndex;
  1752.     }
  1753.     } else if (string[0] == 's') {
  1754.     if (entryPtr->selectFirst == -1) {
  1755.         interp->result = "selection isn't in entry";
  1756.         return TCL_ERROR;
  1757.     }
  1758.     if (length < 5) {
  1759.         goto badIndex;
  1760.     }
  1761.     if (strncmp(string, "sel.first", length) == 0) {
  1762.         *indexPtr = entryPtr->selectFirst;
  1763.     } else if (strncmp(string, "sel.last", length) == 0) {
  1764.         *indexPtr = entryPtr->selectLast;
  1765.     } else {
  1766.         goto badIndex;
  1767.     }
  1768.     } else if (string[0] == '@') {
  1769.     int x, dummy, roundUp;
  1770.  
  1771.     if (Tcl_GetInt(interp, string+1, &x) != TCL_OK) {
  1772.         goto badIndex;
  1773.     }
  1774.     if (x < entryPtr->inset) {
  1775.         x = entryPtr->inset;
  1776.     }
  1777.     roundUp = 0;
  1778.     if (x >= (Tk_Width(entryPtr->tkwin) - entryPtr->inset)) {
  1779.         x = Tk_Width(entryPtr->tkwin) - entryPtr->inset - 1;
  1780.         roundUp = 1;
  1781.     }
  1782.     if (entryPtr->numChars == 0) {
  1783.         *indexPtr = 0;
  1784.     } else {
  1785.         *indexPtr = TkMeasureChars(entryPtr->fontPtr,
  1786.             (entryPtr->displayString == NULL) ? entryPtr->string
  1787.             : entryPtr->displayString,
  1788.             entryPtr->numChars, entryPtr->tabOrigin, x,
  1789.             entryPtr->tabOrigin, TK_NEWLINES_NOT_SPECIAL, &dummy);
  1790.     }
  1791.  
  1792.     /*
  1793.      * Special trick:  if the x-position was off-screen to the right,
  1794.      * round the index up to refer to the character just after the
  1795.      * last visible one on the screen.  This is needed to enable the
  1796.      * last character to be selected, for example.
  1797.      */
  1798.  
  1799.     if (roundUp && (*indexPtr < entryPtr->numChars)) {
  1800.         *indexPtr += 1;
  1801.     }
  1802.     } else {
  1803.     if (Tcl_GetInt(interp, string, indexPtr) != TCL_OK) {
  1804.         goto badIndex;
  1805.     }
  1806.     if (*indexPtr < 0){
  1807.         *indexPtr = 0;
  1808.     } else if (*indexPtr > entryPtr->numChars) {
  1809.         *indexPtr = entryPtr->numChars;
  1810.     }
  1811.     }
  1812.     return TCL_OK;
  1813. }
  1814.  
  1815. /*
  1816.  *----------------------------------------------------------------------
  1817.  *
  1818.  * EntryScanTo --
  1819.  *
  1820.  *    Given a y-coordinate (presumably of the curent mouse location)
  1821.  *    drag the view in the window to implement the scan operation.
  1822.  *
  1823.  * Results:
  1824.  *    None.
  1825.  *
  1826.  * Side effects:
  1827.  *    The view in the window may change.
  1828.  *
  1829.  *----------------------------------------------------------------------
  1830.  */
  1831.  
  1832. static void
  1833. EntryScanTo(entryPtr, x)
  1834.     register Entry *entryPtr;        /* Information about widget. */
  1835.     int x;                /* X-coordinate to use for scan
  1836.                      * operation. */
  1837. {
  1838.     int newLeftIndex;
  1839.  
  1840.     /*
  1841.      * Compute new leftIndex for entry by amplifying the difference
  1842.      * between the current position and the place where the scan
  1843.      * started (the "mark" position).  If we run off the left or right
  1844.      * side of the entry, then reset the mark point so that the current
  1845.      * position continues to correspond to the edge of the window.
  1846.      * This means that the picture will start dragging as soon as the
  1847.      * mouse reverses direction (without this reset, might have to slide
  1848.      * mouse a long ways back before the picture starts moving again).
  1849.      */
  1850.  
  1851.     newLeftIndex = entryPtr->scanMarkIndex
  1852.         - (10*(x - entryPtr->scanMarkX))/entryPtr->avgWidth;
  1853.     if (newLeftIndex >= entryPtr->numChars) {
  1854.     newLeftIndex = entryPtr->scanMarkIndex = entryPtr->numChars-1;
  1855.     entryPtr->scanMarkX = x;
  1856.     }
  1857.     if (newLeftIndex < 0) {
  1858.     newLeftIndex = entryPtr->scanMarkIndex = 0;
  1859.     entryPtr->scanMarkX = x;
  1860.     } 
  1861.     if (newLeftIndex != entryPtr->leftIndex) {
  1862.     entryPtr->leftIndex = newLeftIndex;
  1863.     entryPtr->flags |= UPDATE_SCROLLBAR;
  1864.     EntryComputeGeometry(entryPtr);
  1865.     EventuallyRedraw(entryPtr);
  1866.     }
  1867. }
  1868.  
  1869. /*
  1870.  *----------------------------------------------------------------------
  1871.  *
  1872.  * EntrySelectTo --
  1873.  *
  1874.  *    Modify the selection by moving its un-anchored end.  This could
  1875.  *    make the selection either larger or smaller.
  1876.  *
  1877.  * Results:
  1878.  *    None.
  1879.  *
  1880.  * Side effects:
  1881.  *    The selection changes.
  1882.  *
  1883.  *----------------------------------------------------------------------
  1884.  */
  1885.  
  1886. static void
  1887. EntrySelectTo(entryPtr, index)
  1888.     register Entry *entryPtr;        /* Information about widget. */
  1889.     int index;                /* Index of element that is to
  1890.                      * become the "other" end of the
  1891.                      * selection. */
  1892. {
  1893.     int newFirst, newLast;
  1894.  
  1895.     /*
  1896.      * Grab the selection if we don't own it already.
  1897.      */
  1898.  
  1899.     if ((entryPtr->selectFirst == -1) && (entryPtr->exportSelection)) {
  1900.     Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY, EntryLostSelection,
  1901.         (ClientData) entryPtr);
  1902.     }
  1903.  
  1904.     /*
  1905.      * Pick new starting and ending points for the selection.
  1906.      */
  1907.  
  1908.     if (entryPtr->selectAnchor > entryPtr->numChars) {
  1909.     entryPtr->selectAnchor = entryPtr->numChars;
  1910.     }
  1911.     if (entryPtr->selectAnchor <= index) {
  1912.     newFirst = entryPtr->selectAnchor;
  1913.     newLast = index;
  1914.     } else {
  1915.     newFirst = index;
  1916.     newLast = entryPtr->selectAnchor;
  1917.     if (newLast < 0) {
  1918.         newFirst = newLast = -1;
  1919.     }
  1920.     }
  1921.     if ((entryPtr->selectFirst == newFirst)
  1922.         && (entryPtr->selectLast == newLast)) {
  1923.     return;
  1924.     }
  1925.     entryPtr->selectFirst = newFirst;
  1926.     entryPtr->selectLast = newLast;
  1927.     EventuallyRedraw(entryPtr);
  1928. }
  1929.  
  1930. /*
  1931.  *----------------------------------------------------------------------
  1932.  *
  1933.  * EntryFetchSelection --
  1934.  *
  1935.  *    This procedure is called back by Tk when the selection is
  1936.  *    requested by someone.  It returns part or all of the selection
  1937.  *    in a buffer provided by the caller.
  1938.  *
  1939.  * Results:
  1940.  *    The return value is the number of non-NULL bytes stored
  1941.  *    at buffer.  Buffer is filled (or partially filled) with a
  1942.  *    NULL-terminated string containing part or all of the selection,
  1943.  *    as given by offset and maxBytes.
  1944.  *
  1945.  * Side effects:
  1946.  *    None.
  1947.  *
  1948.  *----------------------------------------------------------------------
  1949.  */
  1950.  
  1951. static int
  1952. EntryFetchSelection(clientData, offset, buffer, maxBytes)
  1953.     ClientData clientData;        /* Information about entry widget. */
  1954.     int offset;                /* Offset within selection of first
  1955.                      * character to be returned. */
  1956.     char *buffer;            /* Location in which to place
  1957.                      * selection. */
  1958.     int maxBytes;            /* Maximum number of bytes to place
  1959.                      * at buffer, not including terminating
  1960.                      * NULL character. */
  1961. {
  1962.     Entry *entryPtr = (Entry *) clientData;
  1963.     int count;
  1964.     char *displayString;
  1965.  
  1966.     if ((entryPtr->selectFirst < 0) || !(entryPtr->exportSelection)) {
  1967.     return -1;
  1968.     }
  1969.     count = entryPtr->selectLast - entryPtr->selectFirst - offset;
  1970.     if (count > maxBytes) {
  1971.     count = maxBytes;
  1972.     }
  1973.     if (count <= 0) {
  1974.     return 0;
  1975.     }
  1976.     if (entryPtr->displayString == NULL) {
  1977.     displayString = entryPtr->string;
  1978.     } else {
  1979.     displayString = entryPtr->displayString;
  1980.     }
  1981.     strncpy(buffer, displayString + entryPtr->selectFirst + offset,
  1982.         (size_t) count);
  1983.     buffer[count] = '\0';
  1984.     return count;
  1985. }
  1986.  
  1987. /*
  1988.  *----------------------------------------------------------------------
  1989.  *
  1990.  * EntryLostSelection --
  1991.  *
  1992.  *    This procedure is called back by Tk when the selection is
  1993.  *    grabbed away from an entry widget.
  1994.  *
  1995.  * Results:
  1996.  *    None.
  1997.  *
  1998.  * Side effects:
  1999.  *    The existing selection is unhighlighted, and the window is
  2000.  *    marked as not containing a selection.
  2001.  *
  2002.  *----------------------------------------------------------------------
  2003.  */
  2004.  
  2005. static void
  2006. EntryLostSelection(clientData)
  2007.     ClientData clientData;        /* Information about entry widget. */
  2008. {
  2009.     Entry *entryPtr = (Entry *) clientData;
  2010.  
  2011.     if ((entryPtr->selectFirst != -1) && entryPtr->exportSelection) {
  2012.     entryPtr->selectFirst = -1;
  2013.     entryPtr->selectLast = -1;
  2014.     EventuallyRedraw(entryPtr);
  2015.     }
  2016. }
  2017.  
  2018. /*
  2019.  *----------------------------------------------------------------------
  2020.  *
  2021.  * EventuallyRedraw --
  2022.  *
  2023.  *    Ensure that an entry is eventually redrawn on the display.
  2024.  *
  2025.  * Results:
  2026.  *    None.
  2027.  *
  2028.  * Side effects:
  2029.  *    Information gets redisplayed.  Right now we don't do selective
  2030.  *    redisplays:  the whole window will be redrawn.  This doesn't
  2031.  *    seem to hurt performance noticeably, but if it does then this
  2032.  *    could be changed.
  2033.  *
  2034.  *----------------------------------------------------------------------
  2035.  */
  2036.  
  2037. static void
  2038. EventuallyRedraw(entryPtr)
  2039.     register Entry *entryPtr;        /* Information about widget. */
  2040. {
  2041.     if ((entryPtr->tkwin == NULL) || !Tk_IsMapped(entryPtr->tkwin)) {
  2042.     return;
  2043.     }
  2044.  
  2045.     /*
  2046.      * Right now we don't do selective redisplays:  the whole window
  2047.      * will be redrawn.  This doesn't seem to hurt performance noticeably,
  2048.      * but if it does then this could be changed.
  2049.      */
  2050.  
  2051.     if (!(entryPtr->flags & REDRAW_PENDING)) {
  2052.     entryPtr->flags |= REDRAW_PENDING;
  2053.     Tcl_DoWhenIdle(DisplayEntry, (ClientData) entryPtr);
  2054.     }
  2055. }
  2056.  
  2057. /*
  2058.  *----------------------------------------------------------------------
  2059.  *
  2060.  * EntryVisibleRange --
  2061.  *
  2062.  *    Return information about the range of the entry that is
  2063.  *    currently visible.
  2064.  *
  2065.  * Results:
  2066.  *    *firstPtr and *lastPtr are modified to hold fractions between
  2067.  *    0 and 1 identifying the range of characters visible in the
  2068.  *    entry.
  2069.  *
  2070.  * Side effects:
  2071.  *    None.
  2072.  *
  2073.  *----------------------------------------------------------------------
  2074.  */
  2075.  
  2076. static void
  2077. EntryVisibleRange(entryPtr, firstPtr, lastPtr)
  2078.     Entry *entryPtr;            /* Information about widget. */
  2079.     double *firstPtr;            /* Return position of first visible
  2080.                      * character in widget. */
  2081.     double *lastPtr;            /* Return position of char just after
  2082.                      * last visible one. */
  2083. {
  2084.     char *displayString;
  2085.     int charsInWindow, endX;
  2086.  
  2087.     if (entryPtr->displayString == NULL) {
  2088.     displayString = entryPtr->string;
  2089.     } else {
  2090.     displayString = entryPtr->displayString;
  2091.     }
  2092.     if (entryPtr->numChars == 0) {
  2093.     *firstPtr = 0.0;
  2094.     *lastPtr = 1.0;
  2095.     } else {
  2096.     charsInWindow = TkMeasureChars(entryPtr->fontPtr,
  2097.         displayString + entryPtr->leftIndex,
  2098.         entryPtr->numChars - entryPtr->leftIndex, entryPtr->inset,
  2099.         Tk_Width(entryPtr->tkwin) - entryPtr->inset, entryPtr->inset,
  2100.         TK_AT_LEAST_ONE|TK_NEWLINES_NOT_SPECIAL, &endX);
  2101.     *firstPtr = ((double) entryPtr->leftIndex)/entryPtr->numChars;
  2102.     *lastPtr = ((double) (entryPtr->leftIndex + charsInWindow))
  2103.         /entryPtr->numChars;
  2104.     }
  2105. }
  2106.  
  2107. /*
  2108.  *----------------------------------------------------------------------
  2109.  *
  2110.  * EntryUpdateScrollbar --
  2111.  *
  2112.  *    This procedure is invoked whenever information has changed in
  2113.  *    an entry in a way that would invalidate a scrollbar display.
  2114.  *    If there is an associated scrollbar, then this procedure updates
  2115.  *    it by invoking a Tcl command.
  2116.  *
  2117.  * Results:
  2118.  *    None.
  2119.  *
  2120.  * Side effects:
  2121.  *    A Tcl command is invoked, and an additional command may be
  2122.  *    invoked to process errors in the command.
  2123.  *
  2124.  *----------------------------------------------------------------------
  2125.  */
  2126.  
  2127. static void
  2128. EntryUpdateScrollbar(entryPtr)
  2129.     Entry *entryPtr;            /* Information about widget. */
  2130. {
  2131.     char args[100];
  2132.     int code;
  2133.     double first, last;
  2134.     Tcl_Interp *interp;
  2135.  
  2136.     if (entryPtr->scrollCmd == NULL) {
  2137.     return;
  2138.     }
  2139.  
  2140.     interp = entryPtr->interp;
  2141.     Tcl_Preserve((ClientData) interp);
  2142.     EntryVisibleRange(entryPtr, &first, &last);
  2143.     sprintf(args, " %g %g", first, last);
  2144.     code = Tcl_VarEval(interp, entryPtr->scrollCmd, args, (char *) NULL);
  2145.     if (code != TCL_OK) {
  2146.     Tcl_AddErrorInfo(interp,
  2147.         "\n    (horizontal scrolling command executed by entry)");
  2148.     Tcl_BackgroundError(interp);
  2149.     }
  2150.     Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
  2151.     Tcl_Release((ClientData) interp);
  2152. }
  2153.  
  2154. /*
  2155.  *----------------------------------------------------------------------
  2156.  *
  2157.  * EntryBlinkProc --
  2158.  *
  2159.  *    This procedure is called as a timer handler to blink the
  2160.  *    insertion cursor off and on.
  2161.  *
  2162.  * Results:
  2163.  *    None.
  2164.  *
  2165.  * Side effects:
  2166.  *    The cursor gets turned on or off, redisplay gets invoked,
  2167.  *    and this procedure reschedules itself.
  2168.  *
  2169.  *----------------------------------------------------------------------
  2170.  */
  2171.  
  2172. static void
  2173. EntryBlinkProc(clientData)
  2174.     ClientData clientData;    /* Pointer to record describing entry. */
  2175. {
  2176.     register Entry *entryPtr = (Entry *) clientData;
  2177.  
  2178.     if (!(entryPtr->flags & GOT_FOCUS) || (entryPtr->insertOffTime == 0)) {
  2179.     return;
  2180.     }
  2181.     if (entryPtr->flags & CURSOR_ON) {
  2182.     entryPtr->flags &= ~CURSOR_ON;
  2183.     entryPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
  2184.         entryPtr->insertOffTime, EntryBlinkProc, (ClientData) entryPtr);
  2185.     } else {
  2186.     entryPtr->flags |= CURSOR_ON;
  2187.     entryPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
  2188.         entryPtr->insertOnTime, EntryBlinkProc, (ClientData) entryPtr);
  2189.     }
  2190.     EventuallyRedraw(entryPtr);
  2191. }
  2192.  
  2193. /*
  2194.  *----------------------------------------------------------------------
  2195.  *
  2196.  * EntryFocusProc --
  2197.  *
  2198.  *    This procedure is called whenever the entry gets or loses the
  2199.  *    input focus.  It's also called whenever the window is reconfigured
  2200.  *    while it has the focus.
  2201.  *
  2202.  * Results:
  2203.  *    None.
  2204.  *
  2205.  * Side effects:
  2206.  *    The cursor gets turned on or off.
  2207.  *
  2208.  *----------------------------------------------------------------------
  2209.  */
  2210.  
  2211. static void
  2212. EntryFocusProc(entryPtr, gotFocus)
  2213.     register Entry *entryPtr;    /* Entry that got or lost focus. */
  2214.     int gotFocus;        /* 1 means window is getting focus, 0 means
  2215.                  * it's losing it. */
  2216. {
  2217.     Tcl_DeleteTimerHandler(entryPtr->insertBlinkHandler);
  2218.     if (gotFocus) {
  2219.     entryPtr->flags |= GOT_FOCUS | CURSOR_ON;
  2220.     if (entryPtr->insertOffTime != 0) {
  2221.         entryPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
  2222.             entryPtr->insertOnTime, EntryBlinkProc,
  2223.             (ClientData) entryPtr);
  2224.     }
  2225.     } else {
  2226.     entryPtr->flags &= ~(GOT_FOCUS | CURSOR_ON);
  2227.     entryPtr->insertBlinkHandler = (Tcl_TimerToken) NULL;
  2228.     }
  2229.     EventuallyRedraw(entryPtr);
  2230. }
  2231.  
  2232. /*
  2233.  *--------------------------------------------------------------
  2234.  *
  2235.  * EntryTextVarProc --
  2236.  *
  2237.  *    This procedure is invoked when someone changes the variable
  2238.  *    whose contents are to be displayed in an entry.
  2239.  *
  2240.  * Results:
  2241.  *    NULL is always returned.
  2242.  *
  2243.  * Side effects:
  2244.  *    The text displayed in the entry will change to match the
  2245.  *    variable.
  2246.  *
  2247.  *--------------------------------------------------------------
  2248.  */
  2249.  
  2250.     /* ARGSUSED */
  2251. static char *
  2252. EntryTextVarProc(clientData, interp, name1, name2, flags)
  2253.     ClientData clientData;    /* Information about button. */
  2254.     Tcl_Interp *interp;        /* Interpreter containing variable. */
  2255.     char *name1;        /* Not used. */
  2256.     char *name2;        /* Not used. */
  2257.     int flags;            /* Information about what happened. */
  2258. {
  2259.     register Entry *entryPtr = (Entry *) clientData;
  2260.     char *value;
  2261.  
  2262.     /*
  2263.      * If the variable is unset, then immediately recreate it unless
  2264.      * the whole interpreter is going away.
  2265.      */
  2266.  
  2267.     if (flags & TCL_TRACE_UNSETS) {
  2268.     if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
  2269.         Tcl_SetVar(interp, entryPtr->textVarName, entryPtr->string,
  2270.             TCL_GLOBAL_ONLY);
  2271.         Tcl_TraceVar(interp, entryPtr->textVarName,
  2272.             TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  2273.             EntryTextVarProc, clientData);
  2274.     }
  2275.     return (char *) NULL;
  2276.     }
  2277.  
  2278.     /*
  2279.      * Update the entry's text with the value of the variable, unless
  2280.      * the entry already has that value (this happens when the variable
  2281.      * changes value because we changed it because someone typed in
  2282.      * the entry).
  2283.      */
  2284.  
  2285.     value = Tcl_GetVar(interp, entryPtr->textVarName, TCL_GLOBAL_ONLY);
  2286.     if (value == NULL) {
  2287.     value = "";
  2288.     }
  2289.     if (strcmp(value, entryPtr->string) != 0) {
  2290.     EntrySetValue(entryPtr, value);
  2291.     }
  2292.     return (char *) NULL;
  2293. }
  2294.